<template>
  <div>
    <table cellspacing="0" class="table">
      <thead>
        <tr>
          <th width="50">
            <Checkbox v-model="selectAll" @on-change="onSelectAll" />
          </th>
          <th width="20" />
          <th v-for="item in listData" :key="item.id" :width="item.width">{{ item.title }}</th>
        </tr>
      </thead>
      <tbody v-for="(item, index) in viewData" :key="index">
        <tr>
          <td>
            <Checkbox v-model="item.checked" @on-change="onJoinSaleCheck(item)" />
          </td>
          <td class="cursor-title">
            <Icon
              v-if="item.showChildren"
              type="ios-arrow-down"
              size="20"
              @click="onShowChild(item)"
            />
            <Icon
              v-if="item.productSkuList && item.productSkuList.length && !item.showChildren"
              type="ios-arrow-forward"
              size="20"
              @click="onShowChild(item)"
            />
          </td>
          <td :title="item.code">{{ item.code }}</td>
          <td>-</td>
          <td class="cursor-title" :title="item.title">
            <div class="flex-start">
              <!-- <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg+'_90w'" alt> -->
              <span class="e2">{{ item.title }}</span>
            </div>
          </td>
          <td v-if="isLarge">-</td>
          <td v-if="isLarge">-</td>
          <td v-if="isLarge">-</td>
          <td>
            <div class="e1">{{ getMinMaxValue(item.productSkuList, {index: 'salePrice'}) }}</div>
          </td>
          <td>{{ item.isShelf | status }}</td>
          <td>{{ getSum(item.productSkuList, {index: 'stock'}) }}</td>
          <td>
            <Button size="small" @click="onDeleteSale(index,list)">删除</Button>
          </td>
        </tr>
        <template v-if="item.productSkuList && item.productSkuList.length">
          <tr
            v-for="(child, index1) in item.productSkuList"
            v-show="item.showChildren"
            :key="index1"
          >
            <td />
            <td />
            <td>
              <Checkbox v-model="child.checked" @on-change="onJoinSaleCheck(child,item)" />
            </td>
            <td :title="child.code">{{ child.code }}</td>
            <td :title="item.title" class="cursor-title">
              <div class="flex-start">
                <!-- <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg+'_90w'" alt> -->
                <span class="e2">{{ item.title }}</span>
              </div>
            </td>
            <td v-if="isLarge">-</td>
            <td v-if="isLarge">-</td>
            <td v-if="isLarge">-</td>
            <td>
              <div class="e1">{{ child.salePrice }}</div>
            </td>
            <td>{{ item.isShelf | status }}</td>
            <td>{{ child.stock }}</td>
            <td>
              <Button size="small" @click="onDeleteSale(index1, item.productSkuList,index,list)">删除</Button>
            </td>
          </tr>
        </template>
      </tbody>
    </table>
    <div class="pager">
      <Page
        :total="totalItem"
        :current="currentPage"
        :page-size="pageSize"
        show-sizer
        placement="top"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
      />
    </div>
    <Loader :status="status" @refresh="getData" />
  </div>
</template>
<script>
import {
  getMinValue,
  getMaxValue,
  getSum
} from '@/views/sales/common/TableSaleMethods'
export default {
  filters: {
    status (val) {
      if (val) {
        return '上架'
      } else {
        return '下架'
      }
    }
  },
  props: {
    list: '',
    status: '',
    listData: '',
    isLarge: {
      value: Boolean,
      default: () => false
    }
  },
  data () {
    return {
      selectAll: false,
      totalItem: 1,
      pageSize: window.$pageSize,
      currentPage: 1,
      viewData: []
    }
  },
  watch: {
    list () {
      this.initData()
    }
  },
  created () {
    this.initData()
  },
  methods: {
    initData () {
      this.totalItem = this.list.length
      this.viewData = this.list.slice(0, this.pageSize)
    },
    getSum (param, object) {
      return getSum(param, object)
    },
    getMinMaxValue (param, object) {
      if (getMinValue(param, object) == getMaxValue(param, object)) {
        return `[${getMinValue(param, object)}]`
      } else {
        return `[${getMinValue(param, object)}-${getMaxValue(param, object)}]`
      }
    },
    onSelectAll () {
      // 全选 、全不选
      this.$emit('onChangeSelectAll', this.selectAll)
    },
    onJoinSale (child, item) {
      this.$emit('onJoinSale', child, item)
    },
    onShowChild (item) {
      item.showChildren = !item.showChildren
    },
    onJoinSaleCheck (child, item) {
      this.$emit('onJoinSaleCheck', child, item)
    },
    onDeleteSale (index, data, index1, data1) {
      this.$emit('onDeleteSale', index, data, index1, data1)
    },
    getData () {},
    onPageChange (val) {
      this.viewData = this.list.slice(
        (val - 1) * this.pageSize,
        val * this.pageSize
      )
    },
    onPageSizeChange (val) {
      this.pageSize = val
      this.viewData = this.list.slice(0, val)
    }
  }
}
</script>

